<template>
  <!-- 应用管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="应用名称:" prop="appName">
            <el-input
              v-model.trim="filterForm.appName"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
          <el-form-item label="应用编码:" prop="appCode">
            <el-input
              v-model.trim="filterForm.appCode"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>

      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['AUTH_APP_ADD_BUTTON']"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="listLoading"
          :data="list"
          border
          highlight-current-row
          :row-key="getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="headerCellStyle"
          @sort-change="customSorting"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="table_index"
          />
          <el-table-column
            v-if="columns[0].show"
            key="appName"
            prop="appName"
            label="应用名称"
            sortable
          />
          <el-table-column
            v-if="columns[1].show"
            key="appCode"
            prop="appCode"
            label="应用编码"
            sortable
          />
          <el-table-column
            v-if="columns[2].show"
            key="appIcon"
            prop="appIcon"
            label="图标"
            sortable
          >
            <template slot-scope="scope">
              <item v-if="scope.row.appIcon" :icon="scope.row.appIcon" />
            </template>
          </el-table-column>
          <!-- <el-table-column
            v-if="columns[3].show"
            prop="activeFlag"
            label="是否置顶"
            sortable
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.activeFlag"
                class="tableScopeSwitch"
                prop="activeFlag"
                active-value="Y"
                inactive-value="N"
                :disabled="scope.row.statusFlag != 1"
                @change="activeFlagChange(scope.row)"
              />
            </template>
          </el-table-column> -->
          <el-table-column
            v-if="columns[3].show"
            prop="appSort"
            label="排序"
            sortable
          />
          <el-table-column
            v-if="columns[4].show"
            key="statusFlag"
            prop="statusFlag"
            label="是否启用"
            sortable
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.statusFlag"
                class="tableScopeSwitch"
                prop="statusFlag"
                :active-value="1"
                :inactive-value="2"
                @change="statusFlagChange(scope.row.appId,scope.row.statusFlag)"
              />
            </template>
          </el-table-column>
          <el-table-column
            :key="Math.random()"
            fixed="right"
            label="操作"
            width="144px"
          >
            <template slot-scope="scope">
              <el-button
                v-permission="['AUTH_APP_UPDATE_BUTTON']"
                size="mini"
                @click="handleEdit(scope.row)"
              >修改</el-button>
              <el-button
                v-permission="['AUTH_APP_DEL_BUTTON']"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row.appId,scope.row.appName)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="filterForm.current"
          :limit.sync="filterForm.size"
          @pagination="getList"
        />
      </div>
      <Edit ref="applicationList" />
    </div>
  </div>
</template>
<script>
import applicationApi from '@/framework/api/application'
import Edit from './applicationEdit'
import Item from '@/framework/layout/components/Sidebar/Item'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'
import { Base64 } from 'js-base64'
export default {
  name: 'AuthApp',
  components: {
    Edit,
    Item
  },
  mixins: [dialogColumn],
  data() {
    return {
      list: [],
      total: 0,
      filterForm: {
        appName: '',
        appCode: '',
        current: 1,
        size: 10
      },
      // 列信息
      columns: [
        { prop: 'appName', label: `应用名称`, show: true },
        { prop: 'appCode', label: `应用编码`, show: true },
        { prop: 'appIcon', label: `图标`, show: true },
        { prop: 'appSort', label: `排序`, show: true },
        // { prop: 'delFlag', label: `是否置顶`, show: true },
        { prop: 'statusFlag', label: `是否启用`, show: true }
      ],
      edit: {
        title: '',
        visible: false,
        formData: {}
      },
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      listLoading: false,
      appListLength: 0,
      // 排序
      sort: '',
      order: '',
      // 应用详情
      applicationMsg: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 全部数据排序
    customSorting(parameter) {
      this.sort = parameter.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      if (parameter.order.startsWith('asc')) {
        this.order = 'asc'
      } else {
        this.order = 'desc'
      }
      this.getList()
    },
    // 列表序号
    table_index(index) {
      return (this.filterForm.current - 1) * this.filterForm.size + index + 1
    },
    // 获取页面列表
    getList() {
      var obj = {}
      obj.pageNo = this.filterForm.current
      obj.pageSize = this.filterForm.size
      obj.appName = this.filterForm.appName == null ? '' : Base64.encode(this.filterForm.appName)
      obj.appCode = this.filterForm.appCode == null ? '' : Base64.encode(this.filterForm.appCode)
      this.listLoading = true
      applicationApi.fetchList(obj).then((res) => {
        this.appListLength = res.data.rows.length
        this.list = res.data.rows
        this.total = res.data.totalRows
        this.listLoading = false
      })
    },
    // 搜索
    handleFilter() {
      this.filterForm.current = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.filterFormRef.resetFields('filterForm')
      this.handleFilter()
    },
    // 编辑按钮
    handleEdit(data) {
      this.$refs.applicationList.openEdit(data || this.applicationMsg)
    },
    // 编辑成功
    handleEditClose(obj) {
      this.getList()
      this.applicationMsg = JSON.parse(JSON.stringify(obj))
    },
    // 删除按钮
    handleDelete(id, name) {
      this.$dtMessageBox({ title: '提示', message: '确定要删除“' + name + '”吗?', type: 'warning' }).then(() => {
        applicationApi.deleteApp(id).then((res) => {
          this.$dtMessage({
            type: 'success',
            message: '删除成功'
          })
          if (this.appListLength == 1) {
            this.filterForm.current--
          }
          this.getList()
        })
      }).catch(() => {
        // 取消不做操作
      })
    },

    // 添加按钮
    handleAdd() {
      this.$refs.applicationList.openAdd()
    },
    // 添加成功
    handleAddClose() {
      this.filterForm.current = 1
      this.getList()
    },
    // 刷新列表 用于监听子组件新增编辑后触发
    // closeForm(type) {
    //   this.edit.formData = {}
    //   this.edit.visible = false
    //   if (type == 'commit') {
    //     this.getList()
    //   }
    // },
    activeFlagChange(row) {
      applicationApi.updateActiveFlag(row.appId).then((res) => {
        if (res.code === '00000') {
          this.$message({
            type: 'success',
            message: '激活成功'
          })
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: '激活失败1:' + res.message
          })
          this.getList()
        }
      }).catch(() => {
        this.getList()
      })
    },
    statusFlagChange(appId, statusFlag) {
      applicationApi.updateStatus(appId, statusFlag).then((res) => {
        this.$dtMessage({
          type: 'success',
          message: '更新成功'
        })
        this.getList()
      }).catch(() => {
        this.getList()
      })
    },
    getRowKey(row) {
      return row.appId
    }
  }
}
</script>

<style>
  .tableScopeSwitch .el-switch__label {
    position: absolute;
    display: none;
    padding-left: 4px;
    color: #fff;
  }
  /*打开时文字位置设置*/
  .tableScopeSwitch .el-switch__label--right {
    z-index: 1;
    right: 20px;
  }
  /*关闭时文字位置设置*/
  .tableScopeSwitch .el-switch__label--left {
    z-index: 1;
    left: 18px;
  }
  /*显示文字*/
  .tableScopeSwitch .el-switch__label.is-active {
    display: block;
  }
</style>
